﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AbstractFramework.LogicControl;
using System.Xml;

namespace WpfElevator.ControlPart
{
    public class ControllerFactory
    {
        public static IController getController(int scheduleIndex)
        {
            XmlNodeList scheduleList = getScheduleList();
            if (scheduleIndex > scheduleList.Count - 1)
            {
                return null;
            }

            XmlNode selectedSchdule = scheduleList[scheduleIndex];

            string strClass = selectedSchdule.Attributes["class"].Value;

            Type c = Type.GetType(strClass);//加载类
            Object configObj = Activator.CreateInstance(c);
            return (IController)configObj;
        }

        public static IController getController1(string strClass)
        {
            Type c = Type.GetType(strClass);//加载类
            Object configObj = Activator.CreateInstance(c);
            return (IController)configObj;
        }

        public static XmlNodeList getScheduleList()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("conf\\Elevator.xml");
            XmlNode root = xmlDoc.SelectSingleNode("ElevatorConf");
            XmlNode shedules = root.SelectSingleNode("Schedules");
            XmlNodeList scheduleList = shedules.SelectNodes("Schedule");
            return scheduleList;
        }
    }
}
